<?php

namespace iBrand\Shop\Backend\Repository;

use Prettus\Repository\Eloquent\BaseRepository;
use iBrand\Shop\Backend\Model\City;

class CityRepository extends BaseRepository
{
	public function model()
	{
		return City::class;
	}

	public function getCityPaginated($where = [], $limit = 15)
	{
		$data = $this->scopeQuery(function ($query) use ($where) {
			if (is_array($where) && !empty($where)) {
				foreach ($where as $key => $value) {
					if (is_array($value)) {
						list($operate, $va) = $value;
						$query = $query->where($key, $operate, $va);
					} else {
						$query = $query->where($key, $value);
					}
				}
			}

			return $query->orderBy('id', 'DESC');
		});

		if ($limit == 0) {
			return $data->all();
		} else {
			return $data->paginate($limit);
		}
	}

	/**
	 * 获取所有城市
	 *
	 * @return array
	 */
	public function getCitys()
	{
		$citys = $this->model->get(['id', 'name'])->toArray();
		$data  = [];
		foreach ($citys as $item) {
			$data[$item['id']] = $item['name'];
		}

		return $data;
	}
}